/*
 * TransitionTableModel.java
 *
 * Created on 07 May 2003, 09:29
 * Copyright (C) 2003, Velocityme Partnership
 */

package com.velocityme.client.gui.node.taskstatemachine;

import com.velocityme.client.gui.VectorTableModel;
import com.velocityme.valueobjects.RoleValue;
import com.velocityme.valueobjects.RoleValueToString;
import com.velocityme.valueobjects.StateValue;
import com.velocityme.valueobjects.StateValueToString;
import com.velocityme.valueobjects.StatusValue;
import com.velocityme.valueobjects.StatusValueToString;
import com.velocityme.valueobjects.TransitionValue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 *
 * @author  Robert Crida Work
 *
 */
public class TransitionTableModel extends VectorTableModel {
    
    private StateValue m_startStateValue = null;
    private StateValue m_defaultStateValue = null;
    private StatusValue m_defaultStatusValue = null;
    private boolean m_editable = false;
    
    /** Creates a new instance of TransitionTableModel */
    public TransitionTableModel() {
        super(new String[] {"Source", "Destination", "Status", "Ower", "User", "Responsible Roles"});
    }
    
    public void setTransitionValues(TransitionValue[] p_values) {
        m_dataRows.clear();
        if (p_values != null) {
            for (int i = 0; i < p_values.length; i++) {
                TransitionValue value = p_values[i];
                StateValue source = new StateValueToString(value.getSourceStateValue());
                StateValue destination = new StateValueToString(value.getDestinationStateValue());
                StatusValue status = new StatusValueToString(value.getStatusValue());
                Boolean owner = value.getIsOwnerResponsible();
                Boolean user = value.getIsUserResponsible();
                Collection roles = new ArrayList();
                RoleValue[] roleValues = value.getResponsibleRoleValues();
                for (int j = 0; j < roleValues.length; j++) {
                    roles.add(new RoleValueToString(roleValues[j]));
                }
                m_dataRows.addElement(new Object[]{ source, destination, status, owner, user, roles, value });
            }
        }
        fireTableRowsInserted(0, m_dataRows.size());
    }
    
    public TransitionValue[] getTransitionValues() {
        TransitionValue[] transitionValues = new TransitionValue[m_dataRows.size()];
        for (int i = 0; i < transitionValues.length; i++)
            transitionValues[i] = getTransitionAt(i);
        return transitionValues;
    }
    
    public TransitionValue getTransitionAt(int row) {
        TransitionValue transitionValue = new TransitionValue((TransitionValue)(((Object[])(m_dataRows.elementAt(row)))[6]));
        transitionValue.setSourceStateValue((StateValue)(((Object[])(m_dataRows.elementAt(row)))[0]));
        transitionValue.setDestinationStateValue((StateValue)(((Object[])(m_dataRows.elementAt(row)))[1]));
        transitionValue.setStatusValue((StatusValue)(((Object[])(m_dataRows.elementAt(row)))[2]));
        transitionValue.setIsOwnerResponsible((Boolean)(((Object[])(m_dataRows.elementAt(row)))[3]));
        transitionValue.setIsUserResponsible((Boolean)(((Object[])(m_dataRows.elementAt(row)))[4]));
        Collection newRoles = (Collection)(((Object[])(m_dataRows.elementAt(row)))[5]);
        RoleValue[] roleValues = new RoleValue[newRoles.size()];
        Iterator i = newRoles.iterator();
        int index = 0;
        while(i.hasNext()) {
            roleValues[index++] = (RoleValue)i.next();
        }
        transitionValue.setResponsibleRoleValues(roleValues);
        return transitionValue;
    }

    public void addValueAt(int index) {
        m_dataRows.add(index, new Object[]{ m_defaultStateValue, m_startStateValue, m_defaultStatusValue, Boolean.FALSE, Boolean.FALSE, new ArrayList(), new TransitionValue() });
        fireTableRowsInserted(index, index);
    }
    
    public void setEditable(boolean editable, StateValue p_startStateValue, StateValue p_defaultStateValue, StatusValue p_defaultStatusValue) {
        m_editable = editable;
        m_startStateValue = p_startStateValue;
        m_defaultStateValue = p_defaultStateValue;
        m_defaultStatusValue = p_defaultStatusValue;
    }
    
    public boolean isCellEditable(int row, int col) {
        return m_editable && !(getValueAt(row,col).equals(m_startStateValue) && col == 0);
    }
    
    public void setValueAt(Object value, int row, int col) {
        ((Object[])m_dataRows.get(row))[col] = value;
        fireTableDataChanged();
    }
    
}
